//
//  DataCallResult.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/6.
//

import UIKit


// MARK: - DataCall
struct OrderModel: Codable, Equatable, Identifiable, Hashable {
    var id : String? = nil
    
    let out_trade_no: String?
    let chepai: String?
    let start_address: String?
    let end_address: String?
    let mydistance: String?
    let lao_price: String?
    let china_price: String?
    let order_type: String?
    let pay_time: String?
    let cash: String?
    let status: String?
    var statusType: String {
        get {
                guard let status = status else {
                    return ""
                }
                
                switch status {
                case "0":
                    return "未接单".localized()
                case "1":
                    return "已接单".localized()
                case "2":
                    return "行程开始".localized()
                case "3":
                    return "行程结束".localized()
                case "4":
                    return "已支付".localized()
                case "-1":
                    return "乘客撤单".localized()
                case "-2":
                    return "司机终止".localized()
                default:
                    return ""
                }
            }
    }
    
    var distanceKM: String {
        get {
            guard mydistance != nil else{
                return "0"
            }
            let number =  Double(mydistance!)!/1000;
            let rounded = Double(round(1000 * number) / 1000)
            return String(rounded)
        }
    }
    
    var payTime: String {
        get {
            guard pay_time != nil else{
                return ""
            }
           let date = Date(timeIntervalSince1970: TimeInterval(pay_time ?? "0")! )
            return date.toStandardString
            
        }
    }
    var cashType: String {
        get {
            guard cash != nil else{
                return "现金".localized()
            }
            if cash! == "1" {
                return "客服微信".localized()
            }
            
            return "现金".localized()
        }
    }
    
    enum CodingKeys: String, CodingKey, Equatable, Hashable {
        case id
        case out_trade_no
        case chepai
        case start_address
        case end_address
        case mydistance
        case lao_price
        case china_price
        case order_type
        case pay_time
        case cash
        case status
        
        
    }
    // 自定义初始化器处理类型转换
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            
            id                  = try values.decodeIfPresent(String.self , forKey: .id                  )
            out_trade_no               = try values.decodeIfPresent(String.self , forKey: .out_trade_no               )
            chepai            = try values.decodeIfPresent(String.self , forKey: .chepai            )
            start_address             = try values.decodeIfPresent(String.self , forKey: .start_address             )
            end_address             = try values.decodeIfPresent(String.self , forKey: .end_address             )
            mydistance               = try values.decodeIfPresent(String.self , forKey: .mydistance               )
            lao_price           = try values.decodeIfPresent(String.self , forKey: .lao_price             )
            china_price           = try values.decodeIfPresent(String.self , forKey: .china_price             )
            order_type           = try values.decodeIfPresent(String.self , forKey: .order_type             )
            pay_time           = try values.decodeIfPresent(String.self , forKey: .pay_time             )
            cash           = try values.decodeIfPresent(String.self , forKey: .cash             )
            status           = try values.decodeIfPresent(String.self , forKey: .status             )
            
            
        }
}
